<?php

/**
 * settings actions.
 *
 * @package    sf_sandbox
 * @subpackage settings
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class settingsActions extends sfActions
{
    public function executeIndex()
    {
        return $this->redirect('settings/categories');
    }

    public function executeCategoriesList()
    {
        $this->cms_categoriess = CmsCategoriesPeer::doSelect(new Criteria());
    }

    public function executeCategoriesCreate()
    {
        $this->cms_categories = new CmsCategories();

        $this->setTemplate('categoriesEdit');
    }

    public function executeCategoriesEdit()
    {
        $this->cms_categories = CmsCategoriesPeer::retrieveByPk($this->getRequestParameter('categories_id'));
        $this->forward404Unless($this->cms_categories);
    }

    public function executeCategoriesUpdate()
    {
        if (!$this->getRequestParameter('categories_id')) {
            $cms_categories = new CmsCategories();
        }
        else
        {
            $cms_categories = CmsCategoriesPeer::retrieveByPk($this->getRequestParameter('categories_id'));
            $this->forward404Unless($cms_categories);
        }

        //$cms_categories->setCategoriesId($this->getRequestParameter('categories_id'));
        $cms_categories->setCategoriesName($this->getRequestParameter('categories_name'));
        $cms_categories->setCategoriesDesc($this->getRequestParameter('categories_desc'));
        $cms_categories->setCategoriesNameCn($this->getRequestParameter('categories_name_cn'));
        $cms_categories->setCategoriesDescCn($this->getRequestParameter('categories_desc_cn'));
        $cms_categories->setStatusCode($this->getRequestParameter('status_code'));
        $cms_categories->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $cms_categories->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        //$cms_categories->setCategoriesPhoto($this->getRequestParameter('categories_photo'));

        $cms_categories->save();
        //var_dump($this->getRequest()->getFileName('fileNew'));
        if ($this->getRequest()->getFileName('fileNew') != '') {
            //exit();
            $uploadedFilename = $this->getRequest()->getFileName('fileNew');
            $ext = explode(".", $this->getRequest()->getFileName('fileNew'));
            $extensionName = $ext[count($ext) - 1];
            //$thumbFile = time().rand(1000,9999).".".$ext[count($ext)-1];
            $filename = "ori-" . $cms_categories->getCategoriesId() . "." . $extensionName;
            $thumbFile = $cms_categories->getCategoriesId() . "." . $extensionName;

            $this->getRequest()->moveFile('fileNew', sfConfig::get('sf_upload_dir') . '/category/' . $filename);
            list($w, $h) = getimagesize('uploads/category/' . $filename);

            $new_height = 177;
            $new_width = $this->getImageResizeWidth($w, $h, $new_height);
            $new_img = imagecreatetruecolor($new_width, $new_height);
            //$img = imagecreatefromjpeg('uploads/category/'.$filename);
            $file_path = 'uploads/category/';
            switch (strtolower(substr(strrchr($uploadedFilename, '.'), 1))) {
                case 'jpg':
                case 'jpeg':

                    $src_img = @imagecreatefromjpeg($file_path . $filename);
                    $write_image = 'imagejpeg';
                    $image_quality = isset($options['jpeg_quality']) ?
                            $options['jpeg_quality'] : 75;

                    imagejpeg($new_img, "uploads/category/" . $thumbFile, 100);
                    break;
                case 'gif':
                    @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                    $src_img = @imagecreatefromgif($file_path . $filename);
                    $write_image = 'imagegif';
                    $image_quality = null;
                    imagegif($new_img, "uploads/category/" . $thumbFile, 100);
                    break;
                case 'png':
                    @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                    @imagealphablending($new_img, false);
                    @imagesavealpha($new_img, true);
                    $src_img = @imagecreatefrompng($file_path . $filename);
                    $write_image = 'imagepng';
                    $image_quality = isset($options['png_quality']) ?
                            $options['png_quality'] : 9;
                    imagepng($new_img, "uploads/category/" . $thumbFile, 100);
                    break;
                default:
                    $src_img = null;
            }
            $success = $src_img && @imagecopyresampled(
                                        $new_img,
                                        $src_img,
                                        0, 0, 0, 0,
                                        $new_width,
                                        $new_height,
                                        $w,
                                        $h
                                    ) && $write_image($new_img, $file_path . $thumbFile, $image_quality);
            imagedestroy($new_img);
            imagedestroy($src_img);

            $cms_categories->setCategoriesPhoto($thumbFile);
            $cms_categories->save();
        }

        $this->setFlash('successMsg', "Categories save successfully");
        return $this->redirect('settings/categoriesEdit?categories_id=' . $cms_categories->getCategoriesId());
    }

    function getImageResizeWidth($width, $height, $maxHeight)
    {
        $percentage = ($height / $maxHeight);

        $width = round($width / $percentage);

        return $width;
    }

    /* ***********************************************
     *      Faq
     * ************************************************/
    public function executeFaqList()
    {
        $this->cms_faqs = CmsFaqPeer::doSelect(new Criteria());
    }

    public function executeFaqCreate()
    {
        $this->cms_faq = new CmsFaq();

        $this->setTemplate('faqEdit');
    }

    public function executeFaqEdit()
    {
        $this->cms_faq = CmsFaqPeer::retrieveByPk($this->getRequestParameter('faq_id'));
        $this->forward404Unless($this->cms_faq);
    }

    public function executeFaqUpdate()
    {
        if (!$this->getRequestParameter('faq_id')) {
            $cms_faq = new CmsFaq();
        }
        else
        {
            $cms_faq = CmsFaqPeer::retrieveByPk($this->getRequestParameter('faq_id'));
            $this->forward404Unless($cms_faq);
        }

        $cms_faq->setFaqTitle($this->getRequestParameter('faq_title'));
        $cms_faq->setFaqAnswer($this->getRequestParameter('faq_answer'));
        $cms_faq->setStatusCode($this->getRequestParameter('status_code'));
        $cms_faq->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $cms_faq->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));

        $cms_faq->save();

        $this->setFlash('successMsg', "Faq save successfully");
        return $this->redirect('settings/faqEdit?faq_id=' . $cms_faq->getFaqId());
    }

    /* ***********************************************
     *      Application Setting
     * ************************************************/
    public function executeApplicationSetting()
    {
        $this->app_settings = AppSettingPeer::doSelect(new Criteria());
    }

    public function executeApplicationSettingUpdate()
    {
        $app_settings = AppSettingPeer::doSelect(new Criteria());
        foreach ($app_settings as $app_setting):
            if (Globals::GOOGLE_ANALYTIC == $app_setting->getParameter()) {
                continue;
            } elseif (Globals::SETTING_BACKGROUND_IMAGE == $app_setting->getParameter()) {
                //var_dump("gege");
                //var_dump($this->getRequest()->getFileName(''.$app_setting->getParameter()));
                //exit();
                if ($this->getRequest()->getFileName(''.$app_setting->getParameter()) != '') {
                    $uploadedFilename = $this->getRequest()->getFileName(''.$app_setting->getParameter());
                    $ext = explode(".", $this->getRequest()->getFileName(''.$app_setting->getParameter()));
                    $extensionName = $ext[count($ext) - 1];
                    //$thumbFile = time().rand(1000,9999).".".$ext[count($ext)-1];
                    $filename = time().rand(1000,9999) . "." . $extensionName;
                    $thumbFile = "small-" . time().rand(1000,9999) . "." . $extensionName;

                    $this->getRequest()->moveFile(''.$app_setting->getParameter(), sfConfig::get('sf_upload_dir') . '/background/' . $filename);
                    list($w, $h) = getimagesize('uploads/background/' . $filename);

                    $new_height = 324;
                    $new_width = $this->getImageResizeWidth($w, $h, $new_height);
                    $new_img = imagecreatetruecolor($new_width, $new_height);
                    //$img = imagecreatefromjpeg('uploads/roundabout/'.$filename);
                    $file_path = 'uploads/background/';
                    switch (strtolower(substr(strrchr($uploadedFilename, '.'), 1))) {
                        case 'jpg':
                        case 'jpeg':

                            $src_img = @imagecreatefromjpeg($file_path . $filename);
                            $write_image = 'imagejpeg';
                            $image_quality = isset($options['jpeg_quality']) ?
                                    $options['jpeg_quality'] : 75;

                            imagejpeg($new_img, "uploads/background/" . $thumbFile, 100);
                            break;
                        case 'gif':
                            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                            $src_img = @imagecreatefromgif($file_path . $filename);
                            $write_image = 'imagegif';
                            $image_quality = null;
                            imagegif($new_img, "uploads/background/" . $thumbFile, 100);
                            break;
                        case 'png':
                            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                            @imagealphablending($new_img, false);
                            @imagesavealpha($new_img, true);
                            $src_img = @imagecreatefrompng($file_path . $filename);
                            $write_image = 'imagepng';
                            $image_quality = isset($options['png_quality']) ?
                                    $options['png_quality'] : 9;
                            imagepng($new_img, "uploads/background/" . $thumbFile, 100);
                            break;
                        default:
                            $src_img = null;
                    }
                    $success = $src_img && @imagecopyresampled(
                                                $new_img,
                                                $src_img,
                                                0, 0, 0, 0,
                                                $new_width,
                                                $new_height,
                                                $w,
                                                $h
                                            ) && $write_image($new_img, $file_path . $thumbFile, $image_quality);
                    imagedestroy($new_img);
                    imagedestroy($src_img);

                    $app_setting->setValue("/uploads/background/".$filename);
                    $app_setting->save();
                }

            } else {
                $app_setting->setValue($this->getRequestParameter($app_setting->getParameter()));
                $app_setting->save();
            }
        endforeach;
        $this->setFlash('successMsg', "Update successfully");

        return $this->redirect('settings/applicationSetting');
    }

    /* ***********************************************
     *      Round About
     * ************************************************/
    public function executeRoundabout()
    {
        $c = new Criteria();
        $c->add(ComponentRoundaboutPeer::COMPONENT_ID, "myRoundabout");
        $roundabout = ComponentRoundaboutPeer::doSelectOne($c);

        $this->component_roundabout_details = ComponentRoundaboutDetailPeer::doSelect(new Criteria());
        $this->roundabout = $roundabout;
    }

    public function executeRoundaboutUpdate()
    {
        $c = new Criteria();
        $c->add(ComponentRoundaboutPeer::COMPONENT_ID, "myRoundabout");
        $roundabout = ComponentRoundaboutPeer::doSelectOne($c);
        $roundabout->setEasing($this->getRequestParameter('easing'));
        $roundabout->setDuration($this->getRequestParameter('duration'));
        $roundabout->save();

        $component_roundabout_details = ComponentRoundaboutDetailPeer::doSelect(new Criteria());

        foreach ($component_roundabout_details as $component_roundabout_detail):
            $component_roundabout_detail->setLinkTarget($this->getRequestParameter('link_target_'.$component_roundabout_detail->getDetailId()));
            $component_roundabout_detail->setImageTitle($this->getRequestParameter('image_title_'.$component_roundabout_detail->getDetailId()));
            $component_roundabout_detail->setImageAlt($this->getRequestParameter('image_alt_'.$component_roundabout_detail->getDetailId()));
            $component_roundabout_detail->setPriceTag($this->getRequestParameter('price_tag_'.$component_roundabout_detail->getDetailId()));

            if ($this->getRequest()->getFileName('fileNew_'.$component_roundabout_detail->getDetailId()) != '') {
                //exit();
                $uploadedFilename = $this->getRequest()->getFileName('fileNew_'.$component_roundabout_detail->getDetailId());
                $ext = explode(".", $this->getRequest()->getFileName('fileNew_'.$component_roundabout_detail->getDetailId()));
                $extensionName = $ext[count($ext) - 1];
                //$thumbFile = time().rand(1000,9999).".".$ext[count($ext)-1];
                $filename = "ori-" . $component_roundabout_detail->getDetailId() . "." . $extensionName;
                $thumbFile = $component_roundabout_detail->getDetailId() . "." . $extensionName;

                $this->getRequest()->moveFile('fileNew_'.$component_roundabout_detail->getDetailId(), sfConfig::get('sf_upload_dir') . '/roundabout/' . $filename);
                list($w, $h) = getimagesize('uploads/roundabout/' . $filename);

                $new_height = 324;
                $new_width = $this->getImageResizeWidth($w, $h, $new_height);
                $new_img = imagecreatetruecolor($new_width, $new_height);
                //$img = imagecreatefromjpeg('uploads/roundabout/'.$filename);
                $file_path = 'uploads/roundabout/';
                switch (strtolower(substr(strrchr($uploadedFilename, '.'), 1))) {
                    case 'jpg':
                    case 'jpeg':

                        $src_img = @imagecreatefromjpeg($file_path . $filename);
                        $write_image = 'imagejpeg';
                        $image_quality = isset($options['jpeg_quality']) ?
                                $options['jpeg_quality'] : 75;

                        imagejpeg($new_img, "uploads/roundabout/" . $thumbFile, 100);
                        break;
                    case 'gif':
                        @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                        $src_img = @imagecreatefromgif($file_path . $filename);
                        $write_image = 'imagegif';
                        $image_quality = null;
                        imagegif($new_img, "uploads/roundabout/" . $thumbFile, 100);
                        break;
                    case 'png':
                        @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                        @imagealphablending($new_img, false);
                        @imagesavealpha($new_img, true);
                        $src_img = @imagecreatefrompng($file_path . $filename);
                        $write_image = 'imagepng';
                        $image_quality = isset($options['png_quality']) ?
                                $options['png_quality'] : 9;
                        imagepng($new_img, "uploads/roundabout/" . $thumbFile, 100);
                        break;
                    default:
                        $src_img = null;
                }
                $success = $src_img && @imagecopyresampled(
                                            $new_img,
                                            $src_img,
                                            0, 0, 0, 0,
                                            $new_width,
                                            $new_height,
                                            $w,
                                            $h
                                        ) && $write_image($new_img, $file_path . $thumbFile, $image_quality);
                imagedestroy($new_img);
                imagedestroy($src_img);

                $component_roundabout_detail->setImageSrc("/uploads/roundabout/".$thumbFile);
                $component_roundabout_detail->setImageWidth($new_width);
                $component_roundabout_detail->setImageHeight($new_height);
            }

            $component_roundabout_detail->save();
        endforeach;
        $this->setFlash('successMsg', "Update successfully");

        return $this->redirect('settings/roundabout');
    }

    /* ***********************************************
   *      Delivery Group
   * ************************************************/
    public function executeDeliveryGroupList()
    {
        $this->delivery_groups = DeliveryGroupPeer::doSelect(new Criteria());
    }

    public function executeDeliveryGroupCreate()
    {
        $this->delivery_group = new DeliveryGroup();

        $this->setTemplate('deliveryGroupEdit');
    }

    public function executeDeliveryGroupEdit()
    {
        $this->delivery_group = DeliveryGroupPeer::retrieveByPk($this->getRequestParameter('delivery_id'));
        $this->forward404Unless($this->delivery_group);
    }

    public function executeDeliveryGroupUpdate()
    {
        if (!$this->getRequestParameter('delivery_id')) {
            $delivery_group = new DeliveryGroup();
        }
        else
        {
            $delivery_group = DeliveryGroupPeer::retrieveByPk($this->getRequestParameter('delivery_id'));
            $this->forward404Unless($delivery_group);
        }

        $delivery_group->setDeliveryId($this->getRequestParameter('delivery_id'));
        $delivery_group->setDeliveryName($this->getRequestParameter('delivery_name'));
        $delivery_group->setDeliveryCharge($this->getRequestParameter('delivery_charge'));
        $delivery_group->setStatusCode($this->getRequestParameter('status_code'));

        $delivery_group->save();

        $this->setFlash('successMsg', "Update successfully");
        return $this->redirect('settings/deliveryGroupEdit?delivery_id=' . $delivery_group->getDeliveryId());
    }
}
